<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>deepClone</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const deepClone = obj => {
            if (obj === null) {
                return null;
            }
            let clone = Object.assign({}, obj);
            Object.keys(clone).forEach(
                key =>
                (clone[key] =
                    typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])
            );
            if (Array.isArray(obj)) {
                clone.length = obj.length;
                return Array.from(clone);
            }
            return clone;
        };
        const a = { foo: 'bar', obj: { a: 1, b: 2 } };
        const b = deepClone(a);
        console.log(a, b, a !== b, a.obj !== b.obj);
    </script>
</body>

</html>